<?php

namespace App\Http\Middleware;

use App\Exceptions\ApiException;
use App\Models\AdminUser;
use Closure;
use Illuminate\Support\Facades\Cookie;


class AdminCheck
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->hasCookie('user_token')) {
            $token = request()->cookie('user_token');
            $decrypted_token = decrypt($token);
            $user = AdminUser::where('token', $decrypted_token)
                ->select('over_time')
                ->first();
            if (!$user || $user->over_time < time()) {
                return response()->json(['code' => 401, 'message' => 'token错误或已失效']);
            }
        } else {
            return response()->json(['code' => 401, 'message' => 'token错误或已失效']);

        }


        return $next($request);
    }


}
